//
//  CustomeAlertView.m
//  U9WatchGame
//
//  Created by 高博昆 on 15/6/12.
//  Copyright (c) 2015年 Jason. All rights reserved.
//

#import "CustomeAlertView.h"
#import "LineView.h"
#import "CustomeOverView.h"

static const CGFloat alertButtonWidth = 250;
static const CGFloat alertButtonHeight = 40;
static const CGFloat alertLabelheight = 40;

@interface CustomeAlertView()

@property (nonatomic, strong)CustomeOverView *overView;
@property (nonatomic, strong)UIWindow *window;

@end

@implementation CustomeAlertView

- (instancetype)initWithType:(int)type{
    if (self = [super init]){
        if (type ==1) {
            CGRect bounds = [self initCommentView];
            self.bounds = bounds;
        } else {
            CGRect bounds = [self initUpdateView];
            self.bounds = bounds;
        }
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 8;
        self.layer.masksToBounds = true;
        
    }
    return self;
}

- (CGRect)initCommentView{
    float positionFir = [self initLabel:0 andText:@"请和所有的中国电子竞技战队"];
    float positionSec = [self initLabel:positionFir andText:@"一起来支持游久电竞"];
    float positionThr = [self initLabel:positionSec andText:@"给个五星好评吧~" ];
    float positionComment = [self initButtonAndLine:positionThr andText:@"马上支持" andTag:100];
    float positionLater = [self initButtonAndLine:positionComment andText:@"稍后再说" andTag:101];
    float positionReject = [self initButtonAndLine:positionLater andText:@"残忍拒绝" andTag:102];
    return CGRectMake(0, 0, alertButtonWidth, positionReject);
}

- (CGRect)initUpdateView{
    float positionFir = [self initLabel:0 andText:@"新版本已发布是否升级?"];
    float positionUpdate = [self initButtonAndLine:positionFir andText:@"马上升级" andTag:103];
    float poistionReject = [self initButtonAndLine:positionUpdate andText:@"稍后升级" andTag:102];
    return CGRectMake(0, 0, alertButtonWidth, poistionReject);
}

- (float)initLabel:(float)positionY andText:(NSString *)text{
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.frame = CGRectMake(0, positionY, alertButtonWidth, alertLabelheight);
    textLabel.text =  text;
    textLabel.textColor = [UIColor colorWithRed:0/255.0 green:166/255.0 blue:235/255.0 alpha:1.0];
    textLabel.font = [UIFont systemFontOfSize:16];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    [self addSubview:textLabel];
    return textLabel.frame.origin.y + alertLabelheight;
}

- (float)initButtonAndLine:(float)positionY andText:(NSString *)text andTag:(int)tag{
    LineView *textLine = [[LineView alloc] initWithFrame:CGRectMake(0, positionY, alertButtonWidth, 0.5) andColor:[UIColor colorWithWhite:0.7 alpha:1.0]];
    [self addSubview:textLine];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, positionY + 0.5, alertButtonWidth, alertButtonHeight)];
    [button setTitle:text forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithRed:0/255.0 green:166/255.0 blue:235/255.0 alpha:1.0] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"UpdateBackGround.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"UpdateHighlightBackGround.png"] forState:UIControlStateHighlighted];
    //button.userInteractionEnabled = true;
    [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = tag;
    [self addSubview:textLine];
    [self addSubview:button];
    return button.frame.origin.y + alertButtonHeight;
}

- (void)popAlertOverView{
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _window.windowLevel = UIWindowLevelStatusBar + 1;
    _window.opaque = false;
    _overView = [CustomeOverView new];
    _overView.opaque = false;
    _overView.frame = _window.bounds;
    _overView.alpha = 0.0;
    [_window addSubview:_overView];
    [_window addSubview:self];
    
    self.center = CGPointMake(_window.bounds.size.width * 0.5, _window.bounds.size.height * 0.5);
    dispatch_async(dispatch_get_main_queue(), ^{
        [_window makeKeyAndVisible];
        [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
            _overView.alpha = 1.0;
        } completion:^(BOOL finished) {
        }];
    });
}

- (void)clickAction:(UIButton *)sender{
    if (sender.tag == 100) {
        [self clickGoComment:sender];
    } else if ( sender.tag == 101) {
        [self clickCancelComment:sender];
    } else if (sender.tag == 102) {
        [self clickLaterComment:sender];
    } else {
        [self clickUpdateApplication:sender];
    }
}


- (void)clickGoComment:(UIButton *)sender{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:true forKey:@"hasComment"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=966817917&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8"] options:[NSDictionary dictionary] completionHandler:nil];
}

- (void)clickLaterComment:(UIButton *)sender{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDateFormatter *fm = [[NSDateFormatter alloc] init];
    [fm setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *date = [fm stringFromDate:[NSDate date]];
    [userDefaults setObject:date forKey:@"laterComment"];
    [self dismiss];
}

- (void)clickCancelComment:(UIButton *)sender{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:true forKey:@"cancelComment"];
    [self dismiss];
}

- (void)clickUpdateApplication:(UIButton *)sender{
    NSString *str = @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=966817917";
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:[NSDictionary dictionary] completionHandler:nil];
    [self dismiss];
}

- (void)dismiss{
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [_overView removeFromSuperview];
        _window = nil;
        _overView = nil;
        [[[[UIApplication sharedApplication] delegate] window] makeKeyAndVisible];
    }];
}



@end
